home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10256 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Date: 15 Mar 1996 21:07:00 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4idi9kINNqbq@keats.ugrad.cs.ubc.ca>
  8. References: <4g0elg$mdr@redstone.interpath.net> <4hqkso$gno@news1.mnsinc.com> <1996Mar15.163722.6685@pyra.co.uk> <4id0t7$9nr@news1.mnsinc.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4id0t7$9nr@news1.mnsinc.com>,
  12. Szu-Wen Huang <huang@mnsinc.com> wrote:
  13. >: May I respectfully suggest that #defines would be clearer and enumerations
  14. >: clearer still ?
  15. >
  16. >No, it won't be, because the states don't always have logical
  17. >significance.  For example, the parser state machine for a simple
  18. >regular expression [A-Za-z]*=[0-9]* might look:
  19. >
  20. >switch (state) {
  21. >  case 0: if (isalpha(input)) ...
  22. >          else if (input == '=') state = 1;
  23. >          break;
  24. >  case 1: if (...
  25. >}
  26. >
  27. >In this case, I don't believe clarify is an issue, first of all,
  28. >because without the state diagram, the code is next to useless
  29. >to a reader (these code tend to be long).  A practical parser
  30. >would have many more states, and #defining those would both be
  31. >a bother and serve no practical purpose in terms of readability.
  32. >In this case, what would you name the states?
  33.  
  34. This is clearly a valid point. But the real source code in this case is the
  35. diagram. In effect, you have compiled the regular expression manually, first
  36. into a state diagram (perhaps going from a non-deterministic one to a
  37. deterministic one via a subset construction), and then a table.
  38.  
  39. The real ``maintainable'' info is in the diagram; the switch statement is just
  40. ``compiled'' code.  I'd tend to use a scanner generator, which can compute the
  41. states and number them just as well as I can!
  42.  
  43. Defining names for the states here is useless, because 0, 1, 2... already
  44. serve as names, effectively. In particular, after you convert a NFA to a DFA,
  45. much of the meaning is lost. The original states of the non-deterministic FA
  46. may have some meaning. They correspond to the regular expression in a
  47. straightforward way. But after the subset construction to produce a
  48. deterministic FA, the states of the DFA don't intuitively correspond to
  49. anything. For instance (fooa|foob) will compress such that as "foo" is read,
  50. both branches are matched simultaneously by the same sequence of three state
  51. changes, and only the reading of a subsequent 'a' or 'b' finally selects a
  52. different state.
  53.  
  54. To blindly follow a rule such as "all constants must be #defined or enumerated
  55. with symbolic names" is pure buffoonery, as is any other "rule of thumb".
  56.  
  57. Rules of thumb are not powerful enough to express anything meaningful in
  58. computer science.
  59. -- 
  60.  
  61.